package com.petShop.controllers;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.petShop.dao.CommentDao;
import com.petShop.dao.UserDao;
import com.petShop.models.Comment;
import com.petShop.models.Userinfo;
import com.petShop.util.MyTool;

@Controller
@Transactional
@RequestMapping("/comment")

public class CommentController {
	@Autowired
	UserDao userDao;
	
	@Autowired
	CommentDao commentDao;
	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView addComment(@RequestParam String content,HttpSession session){
		
		ModelAndView mav = new ModelAndView();
		String username=(String) session.getAttribute("username");
		String returnUrl = (String) session.getAttribute("returnUrl");
		int goodId = (int) session.getAttribute("goodId");
		
		String cont = MyTool.changeHtml(content);
		String cTime = MyTool.changeTime(new Date());
		
		Userinfo userinfo = userDao.selectUserByName(username);
		
		if(userinfo!=null&&cont!=null){
			Comment comment = new Comment();
			comment.setContent(cont);
			comment.setcTime(cTime);
			comment.setUserId(userinfo.getUserId());
			comment.setGoodId(goodId);
			
			commentDao.insertComment(comment);   //添加评论
		}
		
		mav.setViewName("redirect:"+returnUrl);
		return mav;
	}
}
